﻿using System;
using System.Collections.Generic;
using System.Data;

namespace Utility.Helper
{
    public static class ParmsHelper
    {
        public static void Add(this List<Tuple<string, string, string>> parms, string name, object value, string type)
        {
            parms.Add(new Tuple<string, string, string>(name, value == null ? null : value.ToString(), type));
        }

        public static void Add(this List<Tuple<string, string, string>> parms, string name, object value)
        {
            if (value == null)
            {
                parms.Add(new Tuple<string, string, string>(name, value == null ? null : value.ToString(), "string"));
            }
            else
            {
                if (value.GetType().IsAssignableFrom(typeof(string)))
                {
                    parms.Add(new Tuple<string, string, string>(name, value == null ? null : value.ToString(), "string"));
                    return;
                }
                if (value.GetType().IsAssignableFrom(typeof(int)))
                {
                    parms.Add(new Tuple<string, string, string>(name, value == null ? null : value.ToString(), "int"));
                    return;
                }
                if (value.GetType().IsAssignableFrom(typeof(decimal)))
                {
                    parms.Add(new Tuple<string, string, string>(name, value == null ? null : value.ToString(), "decimal"));
                    return;
                }
                if (value.GetType().IsAssignableFrom(typeof(DateTime)))
                {
                    parms.Add(new Tuple<string, string, string>(name, value == null ? null : value.ToString(), "datetime"));
                    return;
                }
                parms.Add(new Tuple<string, string, string>(name, value == null ? null : value.ToString(), "string"));
            }
        }

        public static T GetValue<T>(this DataRow dataRow, string columnName)
        {
            if (dataRow[columnName] == DBNull.Value)
            {
                return default(T);
            }
            if (!typeof(T).IsGenericType)
            {
                return (T)Convert.ChangeType(dataRow[columnName], typeof(T));
            }
            else
            {
                Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition();
                if (genericTypeDefinition == typeof(Nullable<>))
                {
                    return (T)Convert.ChangeType(dataRow[columnName], Nullable.GetUnderlyingType(typeof(T)));
                }
            }
            throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", dataRow[columnName].GetType().FullName, typeof(T).FullName));
        }

        public static List<int> ToIn(this string str)
        {
            var list = new List<int>();
            if (string.IsNullOrEmpty(str))
            {
                return null;
            }
            foreach (var item in str)
            {
                list.Add(Convert.ToInt32(item));
            }
            return list;
        }

        public static string ToSt(this List<int> list)
        {
            var str = "";
            foreach (var item in list)
            {
                if (str != "")
                {
                    str += ",";
                }
                str += item;
            }
            return str;
        }

        public static List<Tuple<string, string, string>> GetParms()
        {
            return new List<Tuple<string, string, string>>();
        }
    }
}